package br.com.expositor.apresentacao.login;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;

import br.com.expositor.apresentacao.administracao.PaginaAdministracao;
import br.com.expositor.apresentacao.infra.imp.PaginaGenerica;
import br.com.expositor.negocios.entidade.Usuario;
import br.com.expositor.repositorio.DAO.entidade.usuario.FiltroUsuario;

public class PaginaLogin extends PaginaGenerica {

	private String login;
	private String senha;

	public PaginaLogin() {
		super();
	}

	private void acaoBotaoLogar() {
		FiltroUsuario filtroUsuario = new FiltroUsuario();
		filtroUsuario.addEquals("login", this.getLogin());
		filtroUsuario.addEquals("senha", this.getSenha());
		Collection<Usuario> usuarios = this.getFachada().consultarUsuario(
				filtroUsuario);
		if (usuarios.size() > 0) {
			this.setUsuarioLogado(((ArrayList<Usuario>) usuarios).get(0));
			this.setResponsePage(new PaginaAdministracao());
		}

	}

	private void criarBtLogin(Form<Component> form) {

		Button login = new Button("logar") {

			private static final long serialVersionUID = 136619695256655094L;

			@Override
			public void onSubmit() {
				PaginaLogin.this.acaoBotaoLogar();
			}
		};

		form.add(login);
	}

	public String getLogin() {
		return this.login;
	}

	public String getSenha() {
		return this.senha;
	}

	@Override
	public void montarPagina() {
		Form<Component> form = new Form<Component>("form");
		form.setOutputMarkupPlaceholderTag(true);
		form.add(new TextField<String>("login"));
		form.add(new PasswordTextField("senha"));
		this.criarBtLogin(form);
		this.add(form);
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

}
